import React, { Component } from 'react'
import ReactDom from 'react-dom'

export default class Life extends Component {
    state = {opacity:1}
    death = () => {
        // 卸载组件
        ReactDom.unmountComponentAtNode(document.getElementById('root'))
    }

    // 组件挂载完毕
    componentDidMount() {
        this.timer = setInterval(() => {
            let { opacity } = this.state;
            opacity -= 0.1;
            if (opacity <= 0) opacity = 1;
            this.setState({ opacity });
        },200)
    }

    // 组件将要卸载
    componentWillUnmount() {
        // 清除定时器
        clearInterval(this.timer);
    }

    render() {
        return (
            <div>
                <h2 style={{opacity:this.state.opacity}}>学不会怎么办？</h2>
                <button onClick={this.death}>不活了</button>
            </div>
        )
    }
}
